//
//  LYCompanyView.m
//  SelectCity
//
//  Created by liuyan on 2019/3/22.
//  Copyright © 2019年 liuyan. All rights reserved.
//


#import "LYCompanyView.h"
#import "LYCompanyCell.h"
#import "UICollectionViewLeftAlignedLayout.h"
#import "PSCompanyCell.h"

@interface LYCompanyView ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
{
    UICollectionView *cCollectionView;
}
@property (nonatomic, strong) NSMutableArray *itemDataArray;
@end

@implementation LYCompanyView

- (instancetype)initWithFrame:(CGRect)frame data:(nonnull NSArray *)dataArray {
    self = [super initWithFrame:frame];
    if (self) {
        self.itemDataArray = [NSMutableArray arrayWithCapacity:0];
        [self.itemDataArray addObjectsFromArray:dataArray];
        [self initUI];
    }
    return self;
}
- (void)initUI {
    UIView *bgView = [UIToolsControl initViewWithFrame:CGRectMake(0, 0, WIDTH, 370*Scale) backgroundColor:COLOR_OF_WHITE];
    [self addSubview:bgView];
    
    UICollectionViewLeftAlignedLayout *layout = [[UICollectionViewLeftAlignedLayout alloc] init];
    layout.minimumInteritemSpacing = 15*Scale;
    layout.minimumLineSpacing = 10*Scale;
    layout.headerReferenceSize = CGSizeMake(WIDTH-30*Scale, 50*Scale);
    layout.footerReferenceSize = CGSizeMake(WIDTH-30*Scale, 10*Scale);
    
    cCollectionView = [UIToolsControl initCollectionViewWithFrame:CGRectMake(15*Scale, 0, WIDTH-30*Scale, 370*Scale) target:self collectionViewLayout:layout collectionViewCellID:isCellID registerClass:[PSCompanyCell class]];
    cCollectionView.showsVerticalScrollIndicator = NO;
    [self addSubview:cCollectionView];
    cCollectionView.backgroundColor = COLOR_OF_WHITE;
    
    [cCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:CollectionViewHeaderViewID];
    [cCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:CollectionViewFooterViewID];
    
    UIButton *resetBtn = [UIToolsControl initButtonWithFrame:CGRectMake(0, EndY(cCollectionView), WIDTH*0.5, 50) imageName:nil target:self action:@selector(resetBtnTap:) title:@"重置" titleColor:THEMECOLOR_OF_TEXT_666666];
    resetBtn.backgroundColor = COLOR_OF_WHITE;
    resetBtn.titleLabel.font = Font(Font_17);
    [resetBtn borderColor:THEMECOLOR_OF_TEXT_cccccc borderWidth:lineH borderType:BorderSideTypeTop];
    [self addSubview:resetBtn];
    
    UIButton *okBtn = [UIToolsControl initButtonWithFrame:CGRectMake(WIDTH/2, StartY(resetBtn), WIDTH*0.5, 50) imageName:nil target:self action:@selector(okBtnTap:) title:@"确定" titleColor:COLOR_OF_WHITE];
    okBtn.backgroundColor = THEMECOLOR_OF_ORANGE;
    okBtn.titleLabel.font = Font(Font_17);
    [okBtn borderColor:THEMECOLOR_OF_TEXT_cccccc borderWidth:lineH borderType:BorderSideTypeTop];
    [self addSubview:okBtn];
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    if (self.itemDataArray && self.itemDataArray.count) {
        return self.itemDataArray.count;
    }
    return 0;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (self.itemDataArray && self.itemDataArray.count) {
        PublicCategoryElementModel *tempModel = self.itemDataArray[section];
        return tempModel.childArray.count;
    }
    return 1;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    PSCompanyCell *cell = (PSCompanyCell *)[collectionView dequeueReusableCellWithReuseIdentifier:isCellID forIndexPath:indexPath];
    if (self.itemDataArray && self.itemDataArray.count) {
        PublicCategoryElementModel *tempModel = self.itemDataArray[indexPath.section];
        [cell reloadData:tempModel.childArray[indexPath.row]];
    }
    
    return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (self.itemDataArray && self.itemDataArray.count) {
        PublicCategoryElementModel *tempModel = self.itemDataArray[indexPath.section];
        PublicCategoryElementModel *tempModelSub = tempModel.childArray[indexPath.row];
        return CGSizeMake(tempModelSub.itemWidth, 40*Scale);
    }
    return CGSizeMake(ceil((WIDTH-60*Scale)/3)-1, 40*Scale);
}
//通过设置SupplementaryViewOfKind 来设置头部或者底部的view，其中 ReuseIdentifier 的值必须和 注册是填写的一致，本例都为 “reusableView”
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:CollectionViewHeaderViewID forIndexPath:indexPath];
        for (UIView *view in headerView.subviews) {
            [view removeFromSuperview];
        }
        
        NSString *titleString = @"";
        if (self.itemDataArray && self.itemDataArray.count) {
            PublicCategoryElementModel *tempModel = self.itemDataArray[indexPath.section];
            titleString = tempModel.name;
        }
        UILabel *label = [UIToolsControl initLabelWithFrame:headerView.bounds text:titleString textColor:THEMECOLOR_OF_TEXT_333333 font:Font_15];
        label.font = FontBold(Font_15);
        [headerView addSubview:label];
        return headerView;
    } else {
        UICollectionReusableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:CollectionViewFooterViewID forIndexPath:indexPath];
        footerView.backgroundColor = COLOR_OF_WHITE;
        return footerView;
    }
    return nil;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (self.itemDataArray && self.itemDataArray.count) {
        PublicCategoryElementModel *tempModel = self.itemDataArray[indexPath.section];
        for (PublicCategoryElementModel *tempModelSub in tempModel.childArray) {
            tempModelSub.isSelect = NO;
        }
        PublicCategoryElementModel *model = self.itemDataArray[indexPath.section];
        PublicCategoryElementModel *modelSub = model.childArray[indexPath.row];
        modelSub.isSelect = YES;
    }
    [collectionView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section]];
}
- (void)resetBtnTap:(UIButton *)button {
    if (self.itemDataArray && self.itemDataArray.count) {
        for (NSInteger i = 0; i < self.itemDataArray.count; i++) {
            PublicCategoryElementModel *tempModel = self.itemDataArray[i];
            for (NSInteger j = 0; j < tempModel.childArray.count; j++) {
                PublicCategoryElementModel *tempModelSub = tempModel.childArray[j];
                if (j == 0) {
                    tempModelSub.isSelect = YES;
                } else {
                    tempModelSub.isSelect = NO;
                }
            }
        }
    }
    [cCollectionView reloadData];
}
- (void)okBtnTap:(UIButton *)button {
    if (self.itemDataArray && self.itemDataArray.count) {
        NSMutableArray *array = [NSMutableArray arrayWithCapacity:0];
        for (NSInteger i = 0; i < self.itemDataArray.count; i++) {
            PublicCategoryElementModel *model = self.itemDataArray[i];
            for (NSInteger j = 0; j < model.childArray.count; j++) {
                PublicCategoryElementModel *tempModelSub = model.childArray[j];
                if (tempModelSub.isSelect) {
                    [array addObject:[tempModelSub copy]];
                }
            }
        }
        
        if (array.count > 0) {
            self.backBlock(array);
        } else {
            self.backBlock(@[]);
        }
    }
}

@end

/*
#import "LYCompanyView.h"
#import "LYCompanyCell.h"

@interface LYCompanyView ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) NSMutableArray *selectArray;

@property (nonatomic, strong) NSMutableArray *itemDataArray;

@end

@implementation LYCompanyView

- (NSMutableArray *)selectArray {
    if (!_selectArray) {
        
        _selectArray = [NSMutableArray array];
    }
    return _selectArray;
}
- (instancetype)initWithFrame:(CGRect)frame data:(nonnull NSArray *)dataArray {
    self = [super initWithFrame:frame];
    if (self) {
        self.itemDataArray = [NSMutableArray arrayWithCapacity:0];
        [self.itemDataArray addObjectsFromArray:dataArray];
        [self createSubview];
    }
    return self;
}

- (void)createSubview{
    CGFloat height = 0;
    for (PublicCategoryElementModel *model in self.itemDataArray) {
        [self.selectArray addObject:@0];
        [self.selectArray addObject:@0];
        [self.selectArray addObject:@0];
//        if (self.selectArray.count) {
//            [self.selectArray addObject:@-1];
//        } else {
//            [self.selectArray addObject:@0];
//        }
        height = height + 55.0;
        NSInteger count = model.childArray.count;
        if (count % 3 == 0) {
            height = height + count/3 * 45;
        } else {
            height = height + (count/3 + 1) * 45;
        }
    }
    
    if (height > 350) {
        height = 350;
    }
    
    self.tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH,height) style:UITableViewStyleGrouped];
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    self.tableview.showsVerticalScrollIndicator = NO;
    self.tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableview.estimatedRowHeight = 0;
    self.tableview.estimatedSectionHeaderHeight = 0;
    self.tableview.estimatedSectionFooterHeight = 0;
    self.tableview.backgroundColor = [UIColor whiteColor];
    self.tableview.bounces = NO;
    [self addSubview:self.tableview];
    [self.tableview registerClass:[LYCompanyCell class] forCellReuseIdentifier:kCellIdentifier_LYCompanyCell];
    
    UIButton *resetBtn = [UIToolsControl initButtonWithFrame:CGRectMake(0, EndY(self.tableview), WIDTH*0.5, 50) imageName:nil target:self action:@selector(resetBtnTap:) title:@"重置" titleColor:THEMECOLOR_OF_TEXT_666666];
    resetBtn.backgroundColor = COLOR_OF_WHITE;
    resetBtn.titleLabel.font = Font(Font_17);
    [resetBtn borderColor:THEMECOLOR_OF_TEXT_cccccc borderWidth:lineH borderType:BorderSideTypeTop];
    [self addSubview:resetBtn];
    
    UIButton *okBtn = [UIToolsControl initButtonWithFrame:CGRectMake(WIDTH/2, EndY(self.tableview), WIDTH*0.5, 50) imageName:nil target:self action:@selector(okBtnTap:) title:@"确定" titleColor:COLOR_OF_WHITE];
    okBtn.backgroundColor = THEMECOLOR_OF_ORANGE;
    okBtn.titleLabel.font = Font(Font_17);
    [okBtn borderColor:THEMECOLOR_OF_TEXT_cccccc borderWidth:lineH borderType:BorderSideTypeTop];
    [self addSubview:okBtn];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (self.itemDataArray && self.itemDataArray.count) {
        return self.itemDataArray.count;
    }
    return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LYCompanyCell *cell =  [tableView dequeueReusableCellWithIdentifier:kCellIdentifier_LYCompanyCell forIndexPath:indexPath];
    cell.backgroundColor = COLOR_OF_CLEAR;
    [cell reloadData:self.itemDataArray[indexPath.row] selectIndex:[self.selectArray[indexPath.row] integerValue]];
    __weak typeof(self) weakSelf = self;
    cell.backlock = ^(NSInteger index){
        [weakSelf companyItemClick:indexPath.row subItem:index];
    };
    return cell;
}

#pragma mark -- UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat height = 55.0;
    PublicCategoryElementModel *model = self.itemDataArray[indexPath.row];
    NSInteger count = model.childArray.count;
    if (count % 3 == 0) {
        height = height + count/3 * 45;
    } else {
        height = height + (count/3 + 1) * 45;
    }
    return height;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.001f;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 5.0;
}

#pragma mark-- Action
- (void)companyItemClick:(NSInteger)section  subItem:(NSInteger)index{
    self.selectArray[section] = [NSNumber numberWithInteger:index];
    [self.tableview reloadData];
}
- (void)resetBtnTap:(UIButton *)button {
    [self.selectArray removeAllObjects];
    for (NSInteger i = 0; i < self.itemDataArray.count; i++) {
        [self.selectArray addObject:@-1];
    }
    [self.tableview reloadData];
}

- (void)okBtnTap:(UIButton *)button {
    NSMutableArray *array = [NSMutableArray arrayWithCapacity:0];
    for (NSInteger i = 0; i < self.itemDataArray.count; i++) {
        PublicCategoryElementModel *model = self.itemDataArray[i];
        NSArray *dataArray = model.childArray;
        NSInteger index = [self.selectArray[i] integerValue];
        if (index >= 0) {
            [array addObject:dataArray[index]];
        }
    }
    if (array.count > 0) {
        self.backBlock(array);
    } else {
        self.backBlock(@[]);
    }
}

@end
*/
